<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">备件编码：</div>
          <el-input
            v-model="sparePartIOCode"
            clearable
            placeholder="请输入备件编码"
            @change="queryPageList()"
          />
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">备件名称：</div>
          <el-input
            v-model="sparePartName"
            clearable
            placeholder="请输入备件名称"
            @change="queryPageList()"
          />
        </div>
        <div class="select-box-content">
          <div class="select-box-content-name">入库日期：</div>
          <el-date-picker
            v-model="ioTime"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            clearable
            @change="queryPageList()"
          />
        </div>
        <div class="operationBtn">
          <div class="screenBtn">
            <div class="btn" @click="changeSelect()">
              <i
                :class="activeName === '' ? 'el-icon-d-arrow-right' : 'el-icon-d-arrow-left'"
                class="arrow-down"
              ></i>
            </div>
          </div>
        </div>

        <el-collapse v-model="activeName" accordion @change="handleChange">
          <el-collapse-item name="1">
            <div class="select-box-content">
              <div class="select-box-content-name">入库人：</div>
              <div
                class="select-box-content-all"
                @click="cancelSelect('username')"
              >全部</div>
              <div
                v-for="(item, index) in userList"
                :key="index"
                class="select-box-content-list"
                :style="{ color: username === item.username ? '#54DEE2' : '#C1CFD2' ,fontWeight : username === item.username ? '600':'400' }"
                @click="selectusername(item.username)"
              >{{ item.username }}</div>
            </div>
          </el-collapse-item>
        </el-collapse>
      </div>
    </template>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :page-no="159"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="tableData"
        rowKey="sparePartIOCode"
        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template
          slot="operate"
          v-if="scoped.row.sparePartIOCode"
          slot-scope="scoped"
        >
          <span
            v-if="judgePermission(159,'deleteone')"
            class="table-btns-box table_del_btn"
            @click="delOne(scoped.row)"
          >
            <i class="el-icon-delete table_del_btn"></i>
            <span>删除</span>
          </span>
        </template>
      </e-table>
      <AddEditDialog
        ref="addEditDialog"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :user-list="userList"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import sparePartsInputApi from "@/api/spare-part-manage/spare-parts-input";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import userApi from "@/api/system-settings/user-manage";
// import dataItemManageApi from "@/api/basic-info-manage/data-item-manage";
import { mapGetters } from "vuex";
export default {
  name: "SparePartsInput",
  components: { ActionBar, BaseMainSlot, AddEditDialog },
  mixins: [mymixin],
  data() {
    return {
      // 所搜条件：备件编码
      sparePartIOCode: "",
      // 所搜条件：备件名称
      sparePartName: "",
      // 所搜条件：入库人
      username: "",
      userList: [],
      // 所搜条件：入库日期
      ioTime: "",
      // startTime: '',
      // endTime: '',
      checkedIdList: [],
      tableLabel: [
        {
          label: "备件编码",
          param: "sparePartIOCode",
          show: true,
        },
        {
          label: "备件名称",
          param: "sparePartName",
          show: true,
        },
        {
          label: "入库数量",
          param: "num",
          show: true,
          notSearch: true,
        },
        {
          label: "入库单价（元）",
          param: "price",
          show: true,
          notSearch: true,
        },
        {
          label: "入库总价（元）",
          param: "total",
          show: true,
          notSearch: true,
        },
        {
          label: "入库人",
          param: "username",
          type: "Select", // input Select Checkbox date
          selectValue: "username", // Select 实际查询参数
          typeObj: {
            // 动态数据和静态数据
            list: [],
            listName: ["username", "username"], // 动态数据和静态数据数组字段名
          },
          show: true,
        },
        {
          label: "存放仓库",
          param: "storeHouseName",
          show: true,
        },
        {
          label: "入库日期",
          param: "ioTime",
          show: true,
          type: "date",
        },
        {
          label: "供应商",
          param: "supplierName",
          show: true,
        },
      ],
      // 表单事件
      tableOption: {
        label: "操作",
        options: [
          {
            label: "操作",
            slotName: "operate",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },

  watch: {},
  created() {
    this.queryUserList();
    this.queryPageList();
    // this.queryUnitId()
  },
  mounted() {},
  methods: {
    // 清空选项
    cancelSelect(data) {
      if (data === "username") {
        this.username = "";
      }
      this.queryPageList();
    },
    // 选择
    selectusername(id) {
      if (this.username === id) {
        this.username = "";
      } else {
        this.username = id;
      }
      this.queryPageList();
    },
    // // 查询单位
    // queryUnitId() {
    //   // 获取单位选择项
    //   dataItemManageApi.queryUnit({}).then((res) => {
    //     if (res.code === 1) {
    //       let data = res.data
    //       this.unitArr = data
    //     } else {
    //       this.$message.error("单位名称给获取失败!");
    //     }
    //   });
    // },
    // selectUnitNameByunitId(v) {
    //   var index = this.unitArr.findIndex(item => item.unitNo === v)
    //   if (index !== -1) {
    //     return this.unitArr[index]['unitName']
    //   }
    // },
    // 查询可选择入库人
    queryUserList() {
      userApi.queryUserAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.userList = data;
          this.setTableColumnsPopover("username", this.userList);
        }
      });
    },
    //表头 下拉框输入赋值
    setTableColumnsPopover(key, data) {
      let item = null;
      let index = 0;
      this.tableLabel.forEach((el, i) => {
        if (el.param === key && el.typeObj) {
          item = el;
          index = i;
          item.typeObj.list = data;
        }
      });
      item && this.$set(this.tableLabel, index, item);
    },
    queryPageList(e) {
      this.loading = false;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          input: 1,
          sparePartIOCode: this.sparePartIOCode,
          sparePartName: this.sparePartName,
          ioTime: this.ioTime,
          username: this.username,
          ...e,
        },
      };
      sparePartsInputApi.queryPageList(param).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data;
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },

    selectAll(list) {
      let notvalue = true;
      list.forEach((item) => {
        if (!item.sparePartIOCode) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      list.forEach((item) => {
        if (!item.sparePartIOCode) {
          notvalue = false;
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map((item) => {
        if (item.sparePartIOCode) {
          return item.sparePartIOCode;
        } else {
          notvalue = false;
          emptyList.push(item);
          return "";
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          var form = new FormData();
          form.append("sparePartIOCodeList", data.sparePartIOCode);
          sparePartsInputApi.delList(form).then((res) => {
            if (res.code === 1) {
              this.$message.success(res.msg);
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 表格删除
    delmessage() {
      var form = new FormData();
      this.checkedIdList.forEach((a) => {
        form.append("sparePartIOCodeList", a);
      });
      sparePartsInputApi.delList(form).then((res) => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.sparePartIOCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";
::v-deep .el-collapse-item__header {
  height: 0px;
}
::v-deep .el-collapse-item__content {
  padding-bottom: 10px;
}
</style>
